﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Weazel.Willows.PreliminaryModel
{
	public class TransitionSet 
	{
		private string sourceStateName;
		private WaitTransition waitTransition;
		private List<Transition> transitions;

		public TransitionSet(XmlElement xmlElement)
		{
			this.transitions = new List<Transition>();
			read(xmlElement);
		}

		#region Read from XML
		private void read(XmlElement description)
		{
			System.Diagnostics.Debug.Assert(description.Name == "TransitionSet");
			System.Diagnostics.Debug.Assert(description.HasAttribute("source"));

			// get the source state name
			this.sourceStateName = description.Attributes["source"].Value;

			if (description["Wait"] != null)
			{
				this.waitTransition = new WaitTransition(description["Wait"]);				
			}

			// read the transition
			foreach(XmlElement element in description.GetElementsByTagName("Transition"))
			{
				Transition transition = new Transition(element);
				this.transitions.Add(transition);
			}
		}
		#endregion

		public void ExpandTo(Weazel.Willows.Model.Module module)
		{
			Weazel.Willows.Model.TransitionSet transitionSet =
				new Weazel.Willows.Model.TransitionSet();

			transitionSet.SourceStateName = sourceStateName;

			// note these are simple copied by reference
			foreach (Transition t in transitions)
				transitionSet.Transitions.Add(t);

			transitionSet.WaitTransition = waitTransition;

			module.TransitionSets.Add(transitionSet);
		}

		public override string ToString()
		{
			return string.Format("Transition set source: {0} transitions: {1} has wait: {2}", sourceStateName, transitions.Count, waitTransition != null);
		}
	}
}
